<?php

namespace core;

class Container
{
    private array $bindings = [];

    public function set(string $id, callable $resolver): void
    {
        $this->bindings[$id] = $resolver;
    }

    public function get(string $id): object
    {
        if (isset($this->bindings[$id])) {
            return $this->bindings[$id]($this);
        }

        // 如果没有绑定规则，尝试直接实例化
        if (class_exists($id)) {
            return new $id();
        }

        throw new \Exception("Class or service not found in container: {$id}");
    }
}
